﻿<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<PageEditViewModel>" %>

<asp:Content ID="indexHead" ContentPlaceHolderID="head" runat="server">
    <title>
        <%= Model.Localize("PageEditSettingsTitle")%></title>
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    <%= Html.SuccessNotification("successNotification", Model.Localize("PageSavedSuccessNotification"))%>
    <%= Html.ErrorNotification("errorNotification", Model.Localize("PageSavedErrorNotification"))%>
    <div class="page-title" id="pages-header">
        <h1>
            <%= Model.Localize("PageEditSettingsHeader")%></h1>
    </div>
    <div class="page-menu">
        <%= Html.UrlActionLink(Model.Localize("BackLink"), Url.Pages())%>
        |
        <%= Html.UrlActionLink(Model.Localize("SaveLink"), "javascript:void(0);", "Save(false);return false;")%>
        |
        <%= Html.UrlActionLink(Model.Localize("SaveAndCloseLink"), "javascript:void(0);", "Save(true);return false;")%>
        |
        <%= Html.UrlActionLink(Model.Localize("ViewLink"), Url.Page(Model.Id), new { target = "_blank" })%>
        |
        <%= Html.UrlActionLink(Model.Localize("DesignLink"), Url.PageTemplate(Model.Id))%>
    </div>
    <div class="settingsPageContainer">
        <% using (Html.BeginForm(Url.EditPage(Model.Id), new { id = "pageEditForm" }))
           {%>
        <% Html.RenderPartial("PageEditControl"); %>
        <%} %>
    </div>

    <script type="text/javascript">
        var redirectBack = false;
        function Save(redirect) {
            redirectBack = redirect;
            var options = {
                success: function(data) {
                    var json = JSON.parse(data);
                    $('.notification').hide('fast');
                    if (json.model != "valid") {
                        $('#errorNotification').show('fast');
                        var result = "<ul>";
                        for (var i in json.errors) {
                            result += "<li>" + json.errors[i] + "</li>";
                        }
                        result += "</ul>";
                        $('#errorNotification .notification-msg-content').html(result);
                    }
                    else {
                        if (redirectBack)
                            window.location.replace('<%= Url.Pages() %>');
                        else
                            $('#successNotification').show('fast');
                    }
                },
                error: function(request, textStatus, errorThrown) {
                    $('.notification').hide('fast');
                    $('#errorNotification').show('fast');
                }
            };
            $('#pageEditForm').ajaxSubmit(options);
        }
        
    </script>

</asp:Content>
